package cn.wolfcode.ssm.controller;

import cn.wolfcode.ssm.domain.CourseOrder;
import cn.wolfcode.ssm.domain.Customer;
import cn.wolfcode.ssm.domain.SystemDictionaryItem;
import cn.wolfcode.ssm.qo.CourseOrderQueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.ICourseOrderService;
import cn.wolfcode.ssm.service.ICustomerService;
import cn.wolfcode.ssm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/courseOrder")
public class CourseOrderController {

    @Autowired
    private ICourseOrderService courseOrderService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ICustomerService customerService;


   @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")CourseOrderQueryObject qo) {
        PageInfo<CourseOrder> pageInfo = courseOrderService.selctForPageResult(qo);
       List<SystemDictionaryItem> courses = systemDictionaryItemService.selectByParentsn("subject");
       List<Customer> customers = customerService.getAll();
       model.addAttribute("pageInfo", pageInfo);
       model.addAttribute("courses", courses);
       model.addAttribute("customers", customers);
        return "courseOrder/list";
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
            if (id != null) {
                courseOrderService.delete(id);
            }
            return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CourseOrder courseOrder) {
            if (courseOrder.getId() != null) {
                courseOrderService.update(courseOrder);
            } else {
                courseOrderService.save(courseOrder);
            }
            return new JsonResult();
    }

}
